package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.common.BasePageInfoAdList;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAd;
import com.cskaoyan.service.promote.AdService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.controller.promote
 * @CreateTime: 2022-11-19  15:41
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("admin/ad")
public class AdController {
    @Autowired
    AdService adService;

    @RequestMapping("list")
    public BaseRespVo adList(BasePageInfoAdList in){
        PageInfo<MarketAd> pageInfo = adService.selectMarketAdByBasePageInfo(in);
        CommonData data = CommonData.data(pageInfo);
        return BaseRespVo.succeed(data);
    }

    @RequestMapping("create")
    public BaseRespVo adCreate(@RequestBody MarketAd marketAd){
        Date date = new Timestamp(System.currentTimeMillis());
        marketAd.setAddTime(date);
        marketAd.setUpdateTime(date);
        adService.insertByMarketAd(marketAd);
        return BaseRespVo.succeed(marketAd);
    }

    @PostMapping("delete")
    public BaseRespVo adDelete(@RequestBody MarketAd marketAd){
        Integer id = marketAd.getId();
        adService.deleteById(id);
        return BaseRespVo.succeed();
    }

    @PostMapping("update")
    public BaseRespVo adUpdate(@RequestBody MarketAd marketAd){
        adService.updateMarketAd(marketAd);
        return BaseRespVo.succeed(marketAd);
    }

}